﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace RibbonEditor.Forms.LocLabels
{
    public partial class LocLabelPicker : Form
    {
        XmlNode locLabelsNode;

        public string SelectedLocLabel { get; set; }

        MainForm linkToMainForm;

        public LocLabelPicker(XmlNode labelsNode, MainForm mainForm)
        {
            InitializeComponent();
            this.linkToMainForm = mainForm;
            this.locLabelsNode = labelsNode;
            this.FillList();

            object o = this.ParentForm;
        }

        private void FillList()
        {
            this.lstItems.Items.Clear();

            if (this.locLabelsNode != null && this.locLabelsNode.SelectNodes("LocLabel") != null)
            {
                foreach (XmlNode locLabelNode in this.locLabelsNode.SelectNodes("LocLabel"))
                {
                    ListViewItem item = new ListViewItem(locLabelNode.Attributes["Id"].Value);
                    item.Tag = locLabelNode;
                    this.lstItems.Items.Add(item);
                }
            }
        }

        private void btnValidate_Click(object sender, EventArgs e)
        {
            if (this.lstItems.SelectedItems.Count > 0)
            {
                XmlNode node = (XmlNode)this.lstItems.SelectedItems[0].Tag;
                this.SelectedLocLabel = node.Attributes["Id"].Value;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show(this, "Please select a localized label!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.SelectedLocLabel = null;
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnProperties_Click(object sender, EventArgs e)
        {
            if (this.lstItems.SelectedItems.Count > 0)
            {
                XmlNode node = (XmlNode)this.lstItems.SelectedItems[0].Tag;
                CreateLocLabelDialog cllDialog = new CreateLocLabelDialog(node);
                cllDialog.StartPosition = FormStartPosition.CenterParent;
                cllDialog.ShowDialog();
            }
        }

        private void btnNewLocLabel_Click(object sender, EventArgs e)
        {
            CreateLocLabelDialog cllDialog = new CreateLocLabelDialog(this.locLabelsNode);

            if (cllDialog.ShowDialog() == DialogResult.OK)
            {
                this.FillList();
                this.linkToMainForm.RefreshLocalizedLabels();
            }
        }

        private void lstItems_DoubleClick(object sender, EventArgs e)
        {
            if (this.lstItems.SelectedItems.Count > 0)
            {
                XmlNode node = (XmlNode)this.lstItems.SelectedItems[0].Tag;
                this.SelectedLocLabel = node.Attributes["Id"].Value;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
